package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.enumclass.ProductType;
import com.rockcent.mall.lib.dto.EvaluationDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by zhangjh on 2016/10/14.
 */
@Repository
public class EvaluationRepositoryImpl implements EvaluationRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(EvaluationRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<EvaluationDto> findProductEvaluationPage(Long mallId, Long merchantId, Integer rangeStar, String beginDate, String endDate, String productType, String keyword, PageResult pageResult) {
        SQLPageDoing<EvaluationDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM evaluation ev left join product p on ev.product_id = p.id where 1=1");
            if (rangeStar != 0) {
                sql.append(" and ev.range_star = :rangeStar");
                params.put("rangeStar", rangeStar);
            }
            if (ProductType.ALL != ProductType.valueOf(productType)) {
                sql.append(" and p.type = :productType");
                params.put("productType", productType);
            }

            if (StringUtils.isNotBlank(keyword)) {
                sql.append(" AND (ev.merchant_order_num LIKE :orderNum or p.id = :id or p.name LIKE :name)");
                params.put("orderNum", "%" + keyword + "%");
                params.put("id", keyword);
                params.put("name", "%" + keyword + "%");
            }

            sql.append(" AND ev.date_created < :endDate AND ev.date_created > :beginDate");
            params.put("beginDate", RDateUtils.beforeDateStr(beginDate, 0));
            params.put("endDate", RDateUtils.afterDateStr(endDate, 1));
            sql.append(" and p.mall_id = :mallId");
            sql.append(" and ev.merchant_id = :merchantId");
            sql.append(" order by ev.date_created desc");
            params.put("mallId", mallId);
            params.put("merchantId", merchantId);
            fields.append("ev.*");
        };
        return doing.go(pageResult, EvaluationDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public Double queryGoodRateByProductId(Long productId) {

        SQLObjectDoing<Double> result = (sql, fields, params, typeReferenceMap) -> {
//            sql.append("select %s FROM intergration_cash_wallet f");
            sql.append("select (select count(1) from evaluation ev where ev.range_star in (4, 5) and  product_id=:productId )/(select count(1) from evaluation ev  where ev.range_star in (1,2,3,4,5) and  product_id=:productId) as goodRate from evaluation ev where ev.product_id=:productId limit 1 ");
            params.put("productId", productId);
        };
        return result.go(Double.class, jdbcTemplate, log, objectMapper);
    }
}
